class Auction < ActiveRecord::Base
  #has_one :dynamic_auction #Bug i Rails!!!
  has_one :fixed_auction
  has_one :fixed, :through => :fixed_auction
  belongs_to :ad
  has_many :bids

  #def initialize
    #@reservation = 0
    #@bid = 0
  #end

  #depricated, use ad.enddate directly insted
  def enddate
    ad.enddate
  end

  #Ugly, but used because of, what looks like, a bug in Rails
  def dynamic_auction
    return DynamicAuction.find_all_by_auction_id(id).first
  end

  def winner
    return Bid.find_by_sql("SELECT bids.* FROM bids WHERE bids.max=(SELECT max(bids.max) FROM bids WHERE bids.auction_id=#{id}) AND bids.auction_id=#{id}").first
  end

  def has_ended
    Time.now > enddate
  end
end
